﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Jzy.Workflow.Flow;
using Jzy.Workflow.Node;
using System.Reflection;
using Jzy.Workflow.Line;
using System.Xml.Linq;

using Frame.Lib.Data;
using Frame.Lib.Core.Base;

namespace Jzy.Workflow.Snap
{
    public class FlowSnapBO : CommonOperate<FlowSnapInfo>
    {
        private FlowSnapDAL _flowSnap;
        protected override BaseDAL DAL
        {
            get
            {
                return _flowSnap;
            }
        }
        public FlowSnapBO()
        {
            _flowSnap = new FlowSnapDAL();
        }

        public FlowSnapBO(DBHelper db):this()
        {
            _flowSnap.dbOper = base.dbOper = db;
        }

        public FlowSnapInfo Save(FlowInfo flowInfo,int id)
        {
            FlowSnapInfo info = new FlowSnapInfo();

            info.ID = id;
            info.FormTemplate = flowInfo.FormTemplate;
            StringBuilder sbUsers = new StringBuilder();
            sbUsers.Append("<NodeUsers>");

            StringBuilder sbRoles = new StringBuilder();
            sbRoles.Append("<NodeRoles>");

            StringBuilder sbNodes = new StringBuilder();            
            sbNodes.Append("<Nodes>");
            foreach (FlowNodeInfo nodeInfo in flowInfo.Nodes)
            {
                sbNodes.Append("<Node>");
                sbNodes.Append("<ID>" + nodeInfo.ID + "</ID>");
                sbNodes.Append("<Name>" + nodeInfo.Name + "</Name>");
                sbNodes.Append("<FlowID>" + nodeInfo.FlowID + "</FlowID>");
                sbNodes.Append("<TypeID>" + nodeInfo.TypeID + "</TypeID>");
                sbNodes.Append("<Left>" + nodeInfo.Left + "</Left>");
                sbNodes.Append("<Top>" + nodeInfo.Top + "</Top>");
                sbNodes.Append("<Width>" + nodeInfo.Width + "</Width>");
                sbNodes.Append("<Height>" + nodeInfo.Height + "</Height>");
                sbNodes.Append("<RoleNames>" + nodeInfo.RoleNames + "</RoleNames>");
                sbNodes.Append("<UserNames>" + nodeInfo.UserNames + "</UserNames>");
                sbNodes.Append("<AllowAdd>" + nodeInfo.AllowAdd + "</AllowAdd>");
                sbNodes.Append("<AllowBack>" + nodeInfo.AllowBack + "</AllowBack>");
                sbNodes.Append("<AllowPass>" + nodeInfo.AllowPass + "</AllowPass>");
                sbNodes.Append("<AllowJump>" + nodeInfo.AllowJump + "</AllowJump>");
                sbNodes.Append("<SubmitMode>" + nodeInfo.SubmitMode + "</SubmitMode>");
                sbNodes.Append("<IsParallel>" + nodeInfo.IsParallel + "</IsParallel>");
                sbNodes.Append("<ParallelMode>" + nodeInfo.ParallelMode + "</ParallelMode>");
                sbNodes.Append("<DefaultUserID>" + nodeInfo.DefaultUserID + "</DefaultUserID>");
                sbNodes.Append("<DefaultNextNodeID>" + nodeInfo.DefaultNextNodeID + "</DefaultNextNodeID>");           
                sbNodes.Append("</Node>");

                foreach(int userID in nodeInfo.UserIDs)
                {
                    sbUsers.Append("<NodeUser>");
                    sbUsers.Append("<NodeID>" + nodeInfo.ID + "</NodeID>");
                    sbUsers.Append("<UserID>" + userID + "</UserID>");
                    sbUsers.Append("</NodeUser>");
                }
               
                foreach(int roleID in nodeInfo.RoleIDs)
                {
                    sbRoles.Append("<NodeRole>");
                    sbRoles.Append("<NodeID>" + nodeInfo.ID + "</NodeID>");
                    sbRoles.Append("<RoleID>" + roleID + "</RoleID>");
                    sbRoles.Append("</NodeRole>");
                }
               
            }
            sbNodes.Append("</Nodes>");
            sbUsers.Append("</NodeUsers>");
            sbRoles.Append("</NodeRoles>");

            info.Nodes = sbNodes.ToString();
            info.NodeRoles = sbRoles.ToString();
            info.NodeUsers = sbUsers.ToString();

            StringBuilder sbLines = new StringBuilder();
            sbLines.Append("<Lines>");
            foreach (FlowLineInfo lineInfo in flowInfo.Lines)
            {
                sbLines.Append("<Line>");
                sbLines.Append("<ID>" + lineInfo.ID + "</ID>");
                sbLines.Append("<Name>" + lineInfo.Name + "</Name>");
                sbLines.Append("<FlowID>" + lineInfo.FlowID + "</FlowID>");
                sbLines.Append("<TypeID>" + lineInfo.TypeID + "</TypeID>");
                sbLines.Append("<FromNodeID>" + lineInfo.FromNodeID + "</FromNodeID>");
                sbLines.Append("<ToNodeID>" + lineInfo.ToNodeID + "</ToNodeID>");
                sbLines.Append(lineInfo.Rules);//已经包含了Rules
                sbLines.Append("</Line>");
            }
            sbLines.Append("</Lines>");

            info.Lines = sbLines.ToString();

            base.Save(info, true);

            return info;
        }

        
    }
}
